﻿using ArmyCreator;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Threading.Tasks;

namespace WebCalls
{
    public class RetrieveListing
    {
        public async static Task Get(int id)
        {
            Caller caller = new Caller();

            Dictionary<string, object> parameters = new Dictionary<string, object>();

            parameters.Add("id", id);

            String result = await caller.Call(Urls.GET_LISTING, parameters);

            var listing = SerializationUtils.Deserialize<DataModel.Listing>(result);

            if (listing != null)
            {
                listing.IsLoaded = true;

                var savedListing = UserContext.Instance.GetListing(id);

                if (savedListing.Army.Id == listing.Army.Id)
                {
                    UserContext.Instance.Listings.Remove(savedListing);
                    UserContext.Instance.Listings.Add(listing);
                }
            }
        }
    }
}
